package com.hakim.wfb.platform.controller;

import com.alibaba.fastjson.JSON;
import com.hakim.wfb.customer.params.recharge.GetUserRechargeWebPageParameter;
import com.hakim.wfb.customer.params.recharge.WalletRechargeParameter;
import com.hakim.wfb.customer.result.recharge.UserRechargeRecordView;
import com.hakim.wfb.customer.result.wallet.UserWalletView;
import com.hakim.wfb.customer.service.RechargeService;
import com.hkfs.fundamental.api.data.ListResultEx;
import com.hkfs.fundamental.api.data.ObjectResultEx;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.api.params.SessionIdentity;
import com.hkfs.fundamental.servlet.utils.ActionUtils;
import com.hkfs.fundamental.session.SessionHandler;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * Created by Administrator on 2017/8/4.
 */
@RequestMapping(value = "/recharge", name = "充值")
@RestController
public class RechargeController {

    private static Logger logger = LoggerFactory.getLogger(RechargeController.class);

    @Autowired
    private RechargeService rechargeService;

    /**
     * 钱包提币成功通知接口
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/notify", name = "钱包充值结果通知")
    public void withdrawNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String requestContent = ActionUtils.getContentFromRequest(request);
        logger.info("钱包充值结果通知." + requestContent);
        logger.info("钱包充值地址"+ ActionUtils.getIpAddr(request));
        logger.info("请求参数"+request.getQueryString());
        try {
          WalletRechargeParameter rechargeParameter = new WalletRechargeParameter();
            rechargeParameter.setUserid(ActionUtils.getInt(request,"userid"));
            rechargeParameter.setAmount(BigDecimal.valueOf(ActionUtils.getDouble(request,"amount")));
            rechargeParameter.setSign(ActionUtils.getString(request,"sign"));
            if (rechargeParameter != null) {
                ResultEx resultEx = rechargeService.recharge(rechargeParameter);
                if (resultEx.isSuccess()) {
                    response.getWriter().print("success");
                }
            } else {
                response.getWriter().print("fail");
            }
        } catch (Exception e) {
            logger.error("Error occurred." + e.getMessage());
        }
    }

    /**
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/records", name = "钱包充值记录",method = RequestMethod.POST)
    public ListResultEx<UserRechargeRecordView> getUserRechargeRecord(GetUserRechargeWebPageParameter param,HttpServletRequest request)
    {
        ListResultEx<UserRechargeRecordView> resultObjectResultEx = new ListResultEx<>();
        try {

            SessionHandler handler = new SessionHandler();
            SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
            param.setUserId(identity.getUuid());
            resultObjectResultEx =rechargeService.queryRechargeRecord(param);

        } catch (Exception e) {
            logger.error("Error occurred." + e.getMessage());
        }
        return resultObjectResultEx;
    }
}
